/**
 * Class utils
 * 
 * @author Hussein Patwa
 * @date 29 April 2008 - Rev 01 May 2008
 * @version 2.0
 * @deprecated Functions.java 1.0 Misc util functions for regression and
 *             correlation operations
 */
package trend;

import java.util.*;

/**
 * @author ssripada
 */
@SuppressWarnings("all")
public class Utils {

  /** Creates a new instance of Utils */
  public Utils() {
  }

  /**
   * Sigma operation on the X co-ordinate
   * 
   * @param dateSet Set on which to operate
   * @return double
   */
  static public double sigmaX(Set dateSet) {
    double rtn = 0;
    int j = 0;
    Double first = new Double(0);
    for (Iterator i = dateSet.iterator(); i.hasNext();) {
      Double key = (Double) i.next();
      if (j == 0)
        first = key;
      rtn = rtn + (key.doubleValue() - first.doubleValue());
      j++;
    }
    return rtn;
  }

  /**
   * Sigma operation on the X co-ordinate squared
   * 
   * @param dateSet Set on which to operate
   * @return double
   */
  static public double sigmaXSquare(Set dateSet) {
    double rtn = 0;
    int j = 0;
    Double first = new Double(0);
    for (Iterator i = dateSet.iterator(); i.hasNext();) {
      Double key = (Double) i.next();
      if (j == 0)
        first = key;
      rtn = rtn
          + Math.pow((key.doubleValue() - first.doubleValue()), 2);
      j++;
    }
    return rtn;
  }

  /**
   * Sigma operation on the Y co-ordinate
   * 
   * @param values Set on which to operate
   * @return double
   */
  static public double sigmaY(Collection values) {
    double rtn = 0;
    for (Iterator i = values.iterator(); i.hasNext();) {
      Double ctVal = (Double) i.next();
      rtn = rtn + ctVal.doubleValue();
    }
    return rtn;
  }

  /**
   * Sigma operation on the Y co-ordinate squared
   * 
   * @param values Set on which to operate
   * @return double
   */
  static public double sigmaYSquare(Collection values) {
    double rtn = 0;
    for (Iterator i = values.iterator(); i.hasNext();) {
      Double ctVal = (Double) i.next();
      rtn = rtn + Math.pow(ctVal.doubleValue(), 2);
    }
    return rtn;
  }

  /**
   * Sigma operation on the X and Y co-ordinate
   * 
   * @param series The tuples XY on which to operate
   * @return double
   */
  static public double sigmaXY(TreeMap series) {
    double rtn = 0;
    Double first = (Double) series.firstKey();
    for (Iterator i = series.entrySet().iterator(); i.hasNext();) {
      Map.Entry e = (Map.Entry) i.next();
      Double key = (Double) e.getKey();
      Double ctVal = (Double) e.getValue();
      rtn = rtn
          + ((key.doubleValue() - first.doubleValue()) * ctVal
              .doubleValue());
    }
    return rtn;
  }

  /**
   * Get the slope between two sets of co-ordinates
   * 
   * @param sigX Result of sigma on the X co-ordinate
   * @param sigX2 Result of sigma on the X co-ordinate squared
   * @param sigY Result of sigma on the Y co-ordinates
   * @param sigXY Result of sigma on the X and Y co-ordinates
   * @param N Mathematical Integer
   * @return double
   */
  public static double getLeastSquareSlope(double sigX, double sigX2,
      double sigY, double sigXY, int N) {
    double num = ((N * sigXY) - (sigX * sigY));
    double den = ((N * sigX2) - Math.pow(sigX, 2));
    return num / den;
  }

  /**
   * Get the least squared from X and Y co-ordinates
   * 
   * @param sigX Result of sigma on the X co-ordinate
   * @param sigX2 Result of sigma on the X co-ordinate squared
   * @param sigY Result of sigma on the Y co-ordinate
   * @param sigXY Result of sigma on the X and Y co-ordinates
   * @param N Mathematical Integer
   * @return double
   */
  public static double getLeastSquareC(double sigX, double sigX2,
      double sigY, double sigXY, int N) {
    double num = ((sigX2 * sigY) - (sigXY * sigX));
    double den = ((N * sigX2) - Math.pow(sigX, 2));
    return num / den;
  }
}
